package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.YzlShieldGuidanceParametersBo;
import org.dromara.system.domain.vo.YzlShieldGuidanceParametersVo;
import org.dromara.system.domain.YzlShieldGuidanceParameters;
import org.dromara.system.mapper.YzlShieldGuidanceParametersMapper;
import org.dromara.system.service.IYzlShieldGuidanceParametersService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 盾构机 导向参数（非双护盾）Service业务层处理
 *
 * @author wfy
 * @date 2025-10-10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class YzlShieldGuidanceParametersServiceImpl implements IYzlShieldGuidanceParametersService {

    private final YzlShieldGuidanceParametersMapper baseMapper;

    /**
     * 查询盾构机 导向参数（非双护盾）
     *
     * @param id 主键
     * @return 盾构机 导向参数（非双护盾）
     */
    @Override
    public YzlShieldGuidanceParametersVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询盾构机 导向参数（非双护盾）列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 盾构机 导向参数（非双护盾）分页列表
     */
    @Override
    public TableDataInfo<YzlShieldGuidanceParametersVo> queryPageList(YzlShieldGuidanceParametersBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<YzlShieldGuidanceParameters> lqw = buildQueryWrapper(bo);
        Page<YzlShieldGuidanceParametersVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的盾构机 导向参数（非双护盾）列表
     *
     * @param bo 查询条件
     * @return 盾构机 导向参数（非双护盾）列表
     */
    @Override
    public List<YzlShieldGuidanceParametersVo> queryList(YzlShieldGuidanceParametersBo bo) {
        LambdaQueryWrapper<YzlShieldGuidanceParameters> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<YzlShieldGuidanceParameters> buildQueryWrapper(YzlShieldGuidanceParametersBo bo) {
        LambdaQueryWrapper<YzlShieldGuidanceParameters> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(YzlShieldGuidanceParameters::getId);
        lqw.eq(bo.getShieldId() != null, YzlShieldGuidanceParameters::getShieldId, bo.getShieldId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), YzlShieldGuidanceParameters::getRemarks, bo.getRemarks());
        lqw.eq(bo.getMeasurementTime() != null, YzlShieldGuidanceParameters::getMeasurementTime, bo.getMeasurementTime());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadx()), YzlShieldGuidanceParameters::getHeadx, bo.getHeadx());
        lqw.eq(StringUtils.isNotBlank(bo.getHeady()), YzlShieldGuidanceParameters::getHeady, bo.getHeady());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadz()), YzlShieldGuidanceParameters::getHeadz, bo.getHeadz());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadHorizontalOffset()), YzlShieldGuidanceParameters::getHeadHorizontalOffset, bo.getHeadHorizontalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadVerticalOffset()), YzlShieldGuidanceParameters::getHeadVerticalOffset, bo.getHeadVerticalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadMileage()), YzlShieldGuidanceParameters::getHeadMileage, bo.getHeadMileage());
        lqw.eq(StringUtils.isNotBlank(bo.getMiddlex()), YzlShieldGuidanceParameters::getMiddlex, bo.getMiddlex());
        lqw.eq(StringUtils.isNotBlank(bo.getMiddley()), YzlShieldGuidanceParameters::getMiddley, bo.getMiddley());
        lqw.eq(StringUtils.isNotBlank(bo.getMiddlez()), YzlShieldGuidanceParameters::getMiddlez, bo.getMiddlez());
        lqw.eq(StringUtils.isNotBlank(bo.getMiddleHorizontalOffset()), YzlShieldGuidanceParameters::getMiddleHorizontalOffset, bo.getMiddleHorizontalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getMiddleVerticalOffset()), YzlShieldGuidanceParameters::getMiddleVerticalOffset, bo.getMiddleVerticalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getMiddleMileage()), YzlShieldGuidanceParameters::getMiddleMileage, bo.getMiddleMileage());
        lqw.eq(StringUtils.isNotBlank(bo.getTailx()), YzlShieldGuidanceParameters::getTailx, bo.getTailx());
        lqw.eq(StringUtils.isNotBlank(bo.getTaily()), YzlShieldGuidanceParameters::getTaily, bo.getTaily());
        lqw.eq(StringUtils.isNotBlank(bo.getTailz()), YzlShieldGuidanceParameters::getTailz, bo.getTailz());
        lqw.eq(StringUtils.isNotBlank(bo.getTailHorizontalOffset()), YzlShieldGuidanceParameters::getTailHorizontalOffset, bo.getTailHorizontalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getTailVerticalOffset()), YzlShieldGuidanceParameters::getTailVerticalOffset, bo.getTailVerticalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getTailMileage()), YzlShieldGuidanceParameters::getTailMileage, bo.getTailMileage());
        lqw.eq(StringUtils.isNotBlank(bo.getRingNumber()), YzlShieldGuidanceParameters::getRingNumber, bo.getRingNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getRollAngle()), YzlShieldGuidanceParameters::getRollAngle, bo.getRollAngle());
        lqw.eq(StringUtils.isNotBlank(bo.getPitchAngle()), YzlShieldGuidanceParameters::getPitchAngle, bo.getPitchAngle());
        lqw.eq(StringUtils.isNotBlank(bo.getHorizontalTrend()), YzlShieldGuidanceParameters::getHorizontalTrend, bo.getHorizontalTrend());
        lqw.eq(StringUtils.isNotBlank(bo.getVerticalTrend()), YzlShieldGuidanceParameters::getVerticalTrend, bo.getVerticalTrend());
        lqw.eq(StringUtils.isNotBlank(bo.getAccumulatedMileage()), YzlShieldGuidanceParameters::getAccumulatedMileage, bo.getAccumulatedMileage());
        lqw.eq(StringUtils.isNotBlank(bo.getTunnelHorizontalTrend()), YzlShieldGuidanceParameters::getTunnelHorizontalTrend, bo.getTunnelHorizontalTrend());
        lqw.eq(StringUtils.isNotBlank(bo.getTunnelVerticalTrend()), YzlShieldGuidanceParameters::getTunnelVerticalTrend, bo.getTunnelVerticalTrend());
        lqw.eq(StringUtils.isNotBlank(bo.getGraphicsHorizontalTrend()), YzlShieldGuidanceParameters::getGraphicsHorizontalTrend, bo.getGraphicsHorizontalTrend());
        lqw.eq(StringUtils.isNotBlank(bo.getGraphicsVerticalTrend()), YzlShieldGuidanceParameters::getGraphicsVerticalTrend, bo.getGraphicsVerticalTrend());
        lqw.eq(StringUtils.isNotBlank(bo.getPredOnemHorizontalOffset()), YzlShieldGuidanceParameters::getPredOnemHorizontalOffset, bo.getPredOnemHorizontalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getPredTwomHorizontalOffset()), YzlShieldGuidanceParameters::getPredTwomHorizontalOffset, bo.getPredTwomHorizontalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getPredThreemHorizontalOffset()), YzlShieldGuidanceParameters::getPredThreemHorizontalOffset, bo.getPredThreemHorizontalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getPredFourmHorizontalOffset()), YzlShieldGuidanceParameters::getPredFourmHorizontalOffset, bo.getPredFourmHorizontalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getPredFivemHorizontalOffset()), YzlShieldGuidanceParameters::getPredFivemHorizontalOffset, bo.getPredFivemHorizontalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getPredOnemVerticalOffset()), YzlShieldGuidanceParameters::getPredOnemVerticalOffset, bo.getPredOnemVerticalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getPredTwomVerticalOffset()), YzlShieldGuidanceParameters::getPredTwomVerticalOffset, bo.getPredTwomVerticalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getPredThreemVerticalOffset()), YzlShieldGuidanceParameters::getPredThreemVerticalOffset, bo.getPredThreemVerticalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getPredFourmVerticalOffset()), YzlShieldGuidanceParameters::getPredFourmVerticalOffset, bo.getPredFourmVerticalOffset());
        lqw.eq(StringUtils.isNotBlank(bo.getPredFivemVerticalOffset()), YzlShieldGuidanceParameters::getPredFivemVerticalOffset, bo.getPredFivemVerticalOffset());
        return lqw;
    }

    /**
     * 新增盾构机 导向参数（非双护盾）
     *
     * @param bo 盾构机 导向参数（非双护盾）
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(YzlShieldGuidanceParametersBo bo) {
        YzlShieldGuidanceParameters add = MapstructUtils.convert(bo, YzlShieldGuidanceParameters.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改盾构机 导向参数（非双护盾）
     *
     * @param bo 盾构机 导向参数（非双护盾）
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(YzlShieldGuidanceParametersBo bo) {
        YzlShieldGuidanceParameters update = MapstructUtils.convert(bo, YzlShieldGuidanceParameters.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(YzlShieldGuidanceParameters entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除盾构机 导向参数（非双护盾）信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
